#include <buola/gl/cglyphcache.h>
#include <buola/image/io.h>

namespace buola { namespace gl {

CGlyphCache::CGlyphCache()
    :   mTree({512,512})
    ,   mSize(0)
{
    mTexture.Create2D<img::TFormat_gray8>({512,512});
}

void CGlyphCache::AddGlyph(img::CScaledGlyph *pGlyph)
{
    img::CGlyphCacheEntry *lEntry=mTree.emplace(pGlyph->mImage->Size(),pGlyph);
    CRect_i lRect=mTree.GetRect(*lEntry);

    mTexture.Copy(view_gray8(*pGlyph->mImage),lRect);
    
    mSize++;
}

CRect_f CGlyphCache::GetRect(img::CScaledGlyph *pGlyph)
{
    CRect_i lRect=mTree.GetRect(*pGlyph->mCacheEntry);

    return CRect_f(lRect.l/512.0f,lRect.t/512.0f,lRect.r/512.0f,lRect.b/512.0f);
}
    
/*namespace gl*/ } /*namespace buola*/ }
